﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;

namespace Nirvana
{

    [CustomPropertyDrawer(typeof(ModelMaterialsImportConfig))]
    public sealed class ModelMaterialsImportConfigPropertyDrawer : PropertyDrawer
    {
        private int index = 0;

        public override void OnGUI(Rect pos, SerializedProperty property, GUIContent label)
        {
            EditorGUI.BeginProperty(pos, label, property);

           // EditorGUI.PrefixLabel(pos, GUIUtility.GetControlID(FocusType.Passive), label);
            EditorGUI.indentLevel++;


            SerializedProperty materialLocation = property.FindPropertyRelative("materialLocation");
            SerializedProperty materialSearch = property.FindPropertyRelative("materialSearch");
            SerializedProperty materialName = property.FindPropertyRelative("materialName");
            SerializedProperty useSRGBMaterialColor = property.FindPropertyRelative("useSRGBMaterialColor");


            index = 0;
            DrawInfo(pos);
#if UNITY_2019_1_OR_NEWER
            SerializedProperty materialImportMode = property.FindPropertyRelative("materialImportMode");
            DrawMaterialCreationModeGUI(pos, materialImportMode);
#else
            SerializedProperty importMaterials = property.FindPropertyRelative("importMaterials");
            DrawMaterialCreationModeGUI(pos, importMaterials);
#endif

            DrawMaterialLocationModeGUI(pos, materialLocation);
            DrawMaterialNamingGUI(pos, materialName);
            DrawMaterialSearchGUI(pos, materialSearch);
            int offsetY = 5;
            index = index + 3;
            var r1 = new Rect(pos.x, pos.y + index++ * EditorGUIUtility.singleLineHeight, pos.width, EditorGUIUtility.singleLineHeight*2);
            index++;
            var r2 = new Rect(pos.x, pos.y + offsetY + index++ * EditorGUIUtility.singleLineHeight, pos.width, EditorGUIUtility.singleLineHeight);
            offsetY = offsetY + 2;
            var r3 = new Rect(pos.x, pos.y + offsetY + index++ * EditorGUIUtility.singleLineHeight, pos.width, EditorGUIUtility.singleLineHeight);
            offsetY = offsetY + 2;
            var r4 = new Rect(pos.x, pos.y + offsetY + index++ * EditorGUIUtility.singleLineHeight, pos.width, EditorGUIUtility.singleLineHeight);
           

            EditorGUI.LabelField(r1, new GUIContent("启用此选项可在伽马空间中使用反照率颜色。默认情况下为旧版导入方法启用了此选项，\n对于使用线性颜色空间的项目，请禁用此选项"));
            EditorGUI.PropertyField(r2, useSRGBMaterialColor);


            EditorGUI.indentLevel--;

            EditorGUI.EndProperty();

        }

        /// <summary>
        /// 绘制所有属性字段在GUI上所需的整体长度
        /// </summary>
        /// <param name="property"></param>
        /// <param name="label"></param>
        /// <returns></returns>
        public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
        {
            return 50 * EditorGUIUtility.singleLineHeight;
        }

        private void DrawInfo(Rect pos)
        {
            var r1 = new Rect(pos.x, pos.y + ++index * EditorGUIUtility.singleLineHeight, pos.width, EditorGUIUtility.singleLineHeight);
            var r2 = new Rect(pos.x, pos.y + 5 + (++index) * EditorGUIUtility.singleLineHeight, pos.width, 4 * EditorGUIUtility.singleLineHeight);
            index = index + 4;
            EditorGUI.LabelField(r1, "信息", GUITheme.LabelTitleStyle);
            EditorGUI.HelpBox(r2,
                "Unity2019版本以上已经解决了FBX的Prefab加载时，生成的Default-Material材质球造成的Standard.Parse性能消耗和StandardLab占用内存过高问题。因此，无需去清除FBX的Renderer上的材质球。:\n\n" +
                "以AdressAble系统打ab包时，也没问题，都可以查找到FBX的依赖材质球。因此Location,默认选择Use Embedded Materials即可。", MessageType.None);


        }

        private void DrawMaterialCreationModeGUI(Rect pos, SerializedProperty property)
        {
            index = index + 2;
            int startIndex = index;
            var r1 = new Rect(pos.x, pos.y + ++index * EditorGUIUtility.singleLineHeight, pos.width, EditorGUIUtility.singleLineHeight);
            var r2 = new Rect(pos.x, pos.y + 5 + (++index) * EditorGUIUtility.singleLineHeight, pos.width, 4 * EditorGUIUtility.singleLineHeight);
            index = index + 4;
            int board = 4;
            var wrap = new Rect(r2.x - board, r2.y - board, pos.width + 2 * board, (index - startIndex) * EditorGUIUtility.singleLineHeight + 2 * board);

            var r4 = new Rect(pos.x, r2.y + (5) * EditorGUIUtility.singleLineHeight - 5, pos.width, EditorGUIUtility.singleLineHeight);

            EditorGUI.DrawRect(wrap, new Color(0.25f, 0.25f, 0.25f, 1));
            EditorGUI.LabelField(r1, "希望Unity如何为模型生成或导入材质(2019的选项)", GUITheme.LabelTitleStyle);
            EditorGUI.HelpBox(r2,
            "None: 不使用此模型中嵌入的任何材质。改用 Unity 的默认漫射材质。\n\n" +
            "Standard: Unity应用一组默认规则来生成材质。(默认项)\n\n" +
            "Import via MaterialDescription: Unity 使用 FBX 文件中嵌入的材质描述来生成材质.\n", MessageType.None);
            GUI.color = Color.yellow;
            EditorGUI.PropertyField(r4, property);

            GUI.color = Color.white;
        }

        private void DrawMaterialLocationModeGUI(Rect pos, SerializedProperty property)
        {
            index = index + 2;
            int startIndex = index;
            var r1 = new Rect(pos.x, pos.y + ++index * EditorGUIUtility.singleLineHeight, pos.width, EditorGUIUtility.singleLineHeight);
            var r2 = new Rect(pos.x, pos.y + 5 + (++index) * EditorGUIUtility.singleLineHeight, pos.width, 4 * EditorGUIUtility.singleLineHeight);
            index = index + 4;
            int board = 4;
            var wrap = new Rect(r2.x - board, r2.y - board, pos.width + 2 * board, (index - startIndex) * EditorGUIUtility.singleLineHeight + 2 * board);

            var r4 = new Rect(pos.x, r2.y + (5) * EditorGUIUtility.singleLineHeight - 5, pos.width, EditorGUIUtility.singleLineHeight);

            EditorGUI.DrawRect(wrap, new Color(0.25f, 0.25f, 0.25f, 1));
            EditorGUI.LabelField(r1, "定义如何访问材质和纹理", GUITheme.LabelTitleStyle);
            EditorGUI.HelpBox(r2,
            "Use Embedded Materials: 将导入的材质保持在导入的资源中。(默认项)\n\n" +
            "Use External Materials (Legacy): 将导入的材质提取为外部资源。这是旧版的材质处理方式，会在本地目录创建materials目录保存材质球", MessageType.None);
            GUI.color = Color.yellow;
            EditorGUI.PropertyField(r4, property);

            GUI.color = Color.white;
        }

        private void DrawMaterialNamingGUI(Rect pos, SerializedProperty property)
        {
            index = index + 2;
            int startIndex = index;
            var r1 = new Rect(pos.x, pos.y + ++index * EditorGUIUtility.singleLineHeight, pos.width, EditorGUIUtility.singleLineHeight);
            var r2 = new Rect(pos.x, pos.y + 5 + (++index) * EditorGUIUtility.singleLineHeight, pos.width, 5 * EditorGUIUtility.singleLineHeight);
            index = index + 5;
            int board = 4;
            var wrap = new Rect(r2.x - board, r2.y - board, pos.width + 2 * board, (index - startIndex) * EditorGUIUtility.singleLineHeight + 2 * board);

            var r4 = new Rect(pos.x, r2.y + (6) * EditorGUIUtility.singleLineHeight - 5, pos.width, EditorGUIUtility.singleLineHeight);

            EditorGUI.DrawRect(wrap, new Color(0.25f, 0.25f, 0.25f, 1));
            EditorGUI.LabelField(r1, "定义材质的命名策略", GUITheme.LabelTitleStyle);
            EditorGUI.HelpBox(r2,
            "By Base Texture Name: 使用导入材质的漫射纹理名称来命名材质\n\n" +
            "From Model’s Material: 使用导入材质的名称来命名材质\n\n" +
            "Model Name + Model’s Material: 使用模型文件的名称与导入材质的名称相结合来命名材质", MessageType.None);
            GUI.color = Color.yellow;
            EditorGUI.PropertyField(r4, property);

            GUI.color = Color.white;
        }

        private void DrawMaterialSearchGUI(Rect pos, SerializedProperty property)
        {
            index = index + 2;
            int startIndex = index;
            var r1 = new Rect(pos.x, pos.y + ++index * EditorGUIUtility.singleLineHeight, pos.width, EditorGUIUtility.singleLineHeight);
            var r2 = new Rect(pos.x, pos.y + 5 + (++index) * EditorGUIUtility.singleLineHeight, pos.width, 5 * EditorGUIUtility.singleLineHeight);
            index = index + 5;
            int board = 4;
            var wrap = new Rect(r2.x - board, r2.y - board, pos.width + 2 * board, (index - startIndex) * EditorGUIUtility.singleLineHeight + 2 * board);

            var r4 = new Rect(pos.x, r2.y + (6) * EditorGUIUtility.singleLineHeight - 5, pos.width, EditorGUIUtility.singleLineHeight);

            EditorGUI.DrawRect(wrap, new Color(0.25f, 0.25f, 0.25f, 1));
            EditorGUI.LabelField(r1, "定义Naming的名称时查找现有材质的位置", GUITheme.LabelTitleStyle);
            EditorGUI.HelpBox(r2,
            "Local Materials Folder: 在本地 Materials 子文件夹（即模型文件所在的同一文件夹）中查找现有材质\n\n" +
            "Recursive-Up: 在所有父文件夹（一直向上追溯到 Assets 文件夹）中的所有 Materials 子文件夹中查找现有材质(默认项)\n\n" +
            "Project-Wide: 在所有 Unity 项目文件夹中查找现有材质", MessageType.None);
            GUI.color = Color.yellow;
            EditorGUI.PropertyField(r4, property);

            GUI.color = Color.white;
        }

    }
}

